public class MySingleList implements ILinkedList{
    @Override
    public void addFirst(int data) {
        ListNode nodeTmp = new ListNode(data);
        nodeTmp.next = this.head;
        head = nodeTmp;
    }

    @Override
    public void addLast(int data) {
        ListNode nodeTmp = new ListNode(data);
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = nodeTmp;
    }
    private void check1(int index){
        if (index < 0 || index > size()){
            throw new CheckPosException("下标不正确");
        }
    }
    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index - 1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode nodeTmp = new ListNode(data);
        check1(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == (size())){
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        nodeTmp.next = cur.next;
        cur.next = nodeTmp;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    private void check2(int key) {
        if (key < 0 || key >= size()){
            throw new CheckPosException("下标不正确");
        }
    }
    @Override
    public void removePos(int key) {
        if(head == null){
            System.out.println("链表为空");
            return;
        }
        check2(key);
        if(key == 0){
            head = head.next;
            return;
        }
        ListNode cur = findIndex(key);
        cur.next = cur.next.next;
        return;
    }
    private ListNode search(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.next.val == key){
                break;
            }
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            return;
        }
        ListNode cur = search(key);
        cur.next = cur.next.next;
    }

    @Override
    public void removeAllKey(int key) {
        ListNode pre = head;
        ListNode cur = head.next;
        while(head.val == key){
            head = head.next;
        }
        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
    }


    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        this.head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(45);
        ListNode node3 = new ListNode(56);
        ListNode node4 = new ListNode(66);
        ListNode node5 = new ListNode(70);
        ListNode node6 = new ListNode(23);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        head = node1;
    }
}
